/* 全局变量 */

// 奖励上限
var REWARD_LIMIT = 99999;
var DELAY_TIME = 10000;

$(function() {

	/* 设置页面最小高度 */

	var $content = $("div.content").eq(0);

	var clientHeight = document.documentElement.clientHeight;
	var headerHeight = $("div.header").eq(0).height();
	var wrapperMarginTop = parseInt($("div.wrapper").eq(0).css("margin-top"));
	var contentPaddingTop = parseInt($content.css("padding-top"));
	var $success_message = $("div.success-message");

	$content.css({"min-height": clientHeight - headerHeight - wrapperMarginTop - contentPaddingTop - 2});	// 2 为 $content 的上下边框宽度

	/* 为标签添加样式 */

	//$("div.nav ul.tags > li").each(function() {
	//	$(this).prepend("<span class=\"before\"></span>").append("<span class=\"after\"></span>");
	//})

	/* 功能项目切换 */

	$("div.nav span.nav-item").click(function() {
		var $this = $(this);
		var $li = $this.parent();

		// 收起/展开 tags
		$li.siblings().children("ul.tags").slideUp("normal", function() {
			$li.children("ul.tags").slideDown();

			// 添加/移除 current 样式
			$li.addClass("current").siblings().removeClass("current");
		});
		
	})

	/* 为文本框提示词添加样式 */

	var $home_name_input = $("div.add-task input[type=text], div.add-reward input[type=text]");

	$home_name_input.each(function() {
		if (this.value == this.defaultValue) {
			$(this).addClass("default");
		}
	}).focus(function() {
		$(this).removeClass("default");
		if (this.value == this.defaultValue) {
			$(this).val("");
		}
	}).blur(function() {
		if (this.value == "") {
			$(this).val(this.defaultValue);
			$(this).addClass("default");
		}
	})

	/* 为 checkbox 添加样式 */

	$("label.checkbox input:checkbox, label.radio input:radio").each(function() {
		if (this.checked) {
			$(this).parent().addClass("checked");
		} else {
			$(this).parent().removeClass("checked");
		}
	}).change(function() {
		if (this.checked) {
			$(this).parent().addClass("checked");
			if ($(this).attr("type") == "radio") {
				$(this).parent().parent().siblings().find("label.radio").removeClass("checked");
			}
		} else {
			$(this).parent().removeClass("checked");
		}
	})

	/* 数字加减 */

	$("div.plus").click(function() {
		var $number = $(this).parent().find(":text");
		var current_number = parseInt($number.val());
		if (current_number < REWARD_LIMIT) {
			$number.val(current_number + 1);
		}
	})

	$("div.minus").click(function() {
		var $number = $(this).parent().find(":text");
		var current_number = parseInt($number.val());
		if (current_number > 0) {
			$number.val(current_number - 1);
		}
	})

	/* 调整 radio 的位置 */
	$("ul.radio li").each(function(i) {
		$(this).find("label.radio").css({"left": -1 * i});
	})
	
	$("ul.tasks li :checkbox").live("click",function(e) {
		debugger;
		//var $points = $(this).parent().find("span.coin").text();
		
		php_finishtask($(this).parent().parent(),$success_message);
		
		$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
			"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
			.fadeIn().delay(DELAY_TIME).fadeOut();;
		//e.stopPropagation();
	})

	/* 显示删除/编辑按钮 */
	
	$("ul.tasks li").live("click",function() {
		var $this = $(this);

		$this.siblings().find("div.operation").hide(0, function() {
			$this.siblings().find("span.coin").show();
		});

		var $coin = $this.find("span.coin");
		var $operation = $this.find("div.operation");

		if ($coin.is(":visible")) {
			$coin.hide();
			$operation.fadeIn();
		} else {
			$operation.hide();
			$coin.fadeIn();
		}
		
		setEditableTaskID($operation.find("input.hidden_id").val());
	})

	/* 弹出框 */

	$("div.close, input[value=Cancel]").click(function() {
		$(this).parents(".alert-wrapper").hide();
	})

	/* 添加任务 */

	var $task_name_label = $("div#add-task-box").find("label.task-name");
	var $task_name_input = $("div#add-task-box").find("input.task-name");

	$("input#add").click(function() {
		// 隐藏错误信息
		$("form#task div.error-message").text("").hide();

		// 设置任务名称
		var current_task_name = $home_name_input.val();
		if (current_task_name != $home_name_input[0].defaultValue && current_task_name != "") {
			$task_name_label.text(current_task_name).show();
			$task_name_input.val(current_task_name).hide();
		} else {
			$task_name_label.text("").hide();
			$task_name_input.val("").show();
		}

		// 默认到期为今天 
		var d = new Date();
		$("select[name=year]").val(d.getFullYear());
		
		$m = d.getMonth() + 1 ;
		$mval = $m >= 10 ? $m : "0"+ $m;
		$("select[name=month]").val($mval);
		
		$da = d.getDate();
		$dval = $da >= 10 ? $da : "0"+ $da;
		$("select[name=date]").val($dval);

		// 显示窗口
		$("div#add-task-box").show();
		verticalMiddle($("div#add-task-box").find("div.alert"));
	})

	/* 修改任务名称 */
	$task_name_label.click(function() {
		$(this).hide()
		$task_name_input.val($(this).text()).show();
	})

	$task_name_input.keyup(function(e) {
		var current_task_name = $(this).val();
		$task_name_label.text(current_task_name);
		$home_name_input.val(current_task_name);
		if (e.which == 13 && current_task_name != "") {
			$(this).hide();
			$task_name_label.show();
		}
	})

	/* 确认添加任务 */

	$("div#add-task-box input:submit").click(function() {

		// 验证任务
		var isValid = Validate($("form#task"));

		if (isValid) {			
			$("form#task div.error-message").fadeOut();
			$("div#add-task-box").hide();

			$success_message.text("A new task was added!");
			$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
				"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
				.fadeIn().delay(DELAY_TIME).fadeOut();;
				
			$home_name_input.val($home_name_input[0].defaultValue).addClass("default");
			
			php_addtask($("form#task"),$success_message);
		}
	})

	/* 删除任务 */

	$("input[value=delete]").live("click",function(e) {
		e.stopPropagation();

		$("div#delete-task-box").show();
		
		verticalMiddle($("div#delete-task-box").find("div.alert"));
	})

	/* 确认删除任务 */

	$("div#delete-task-box input:submit").click(function() {
		$("div#delete-task-box").hide();
		
		php_deletetask($success_message);

		$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
			"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
			.fadeIn().delay(DELAY_TIME).fadeOut();
	})

	/* 编辑任务 */

	var $edit_task_box = $("div#edit-task-box");
	$("input[value=edit]").live("click",function(e) {
		e.stopPropagation();
		debugger;
		var $li = $(this).parent().parent();
		var $date = $li.find("span.due-date").text().split('-');
		$edit_task_box.find("input.task-name").val($li.find("p.task-content span.task-name").text()).show();
		$edit_task_box.find("input.set-coin").val($li.find("span.coin").text().replace(/\$/, ""));
		$edit_task_box.find("input.tag").val($li.find("span.tag").text());
		$edit_task_box.find("input.url").val($li.find("a").attr('href'));
		$edit_task_box.find("select[name=year]").val($date[0]);
		$edit_task_box.find("select[name=month]").val($date[1]);
		$edit_task_box.find("select[name=date]").val($date[2]);

		$edit_task_box.show();
		verticalMiddle($edit_task_box.find("div.alert"));
	})

	/* 确认编辑任务 */

	$("div#edit-task-box input:submit").click(function() {

		// 验证任务

		var $form = $edit_task_box.find("form.task");
		var isValid = Validate($form);

		if (isValid) {
			$form.find("div.error-message").fadeOut();
			$("div#edit-task-box").hide();
			
			$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
				"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
				.fadeIn().delay(DELAY_TIME).fadeOut();
			
			php_updatetask($form,$success_message);
		}
	})
	
/**********************************************************************以下是奖励相关*********************************************************************/
/****************************************************************************************************************************************************/
	
	/* 显示删除/编辑按钮 */
	$("ul.rewards li p").live("click",function() {
		var $this = $(this).parent();

		$this.siblings().find("div.operation").hide(0, function() {
			$this.siblings().find("span.coin").show();
		});

		var $coin = $this.find("span.coin");
		var $operation = $this.find("div.operation");

		if ($coin.is(":visible")) {
			$coin.hide();
			$operation.fadeIn();
		} else {
			$operation.hide();
			$coin.fadeIn();
		}
		
		setEditableTaskID($operation.find("input.hidden_id").val());
	})

	/* 添加奖励 */

	var $reward_name_label = $("div#add-reward-box").find("label.reward-name");
	var $reward_name_input = $("div#add-reward-box").find("input.reward-name");

	$("input#addr").click(function() {

		// 隐藏错误信息
		$("form#reward div.error-message").text("").hide();

		// 设置奖励名称
		var current_reward_name = $home_name_input.val();
		if (current_reward_name != $home_name_input[0].defaultValue && current_reward_name != "") {
			$reward_name_label.text(current_reward_name).show();
			$reward_name_input.hide();
		} else {
			$reward_name_label.text("").hide();
			$reward_name_input.val("").show();
		}

		// 显示窗口

		$("div#add-reward-box").show();
		verticalMiddle($("div#add-reward-box").find("div.alert"));
	})

	/* 修改奖励名称 */
	$reward_name_label.click(function() {
		$(this).hide()
		$reward_name_input.val($(this).text()).show();
	})

	$reward_name_input.keyup(function(e) {
		var current_reward_name = $(this).val();
		$reward_name_label.text(current_reward_name);
		$home_name_input.val(current_reward_name);
		if (e.which == 13 && current_reward_name != "") {
			$(this).hide();
			$reward_name_label.show();
		}
	})

	/* 确认添加奖励 */

	var $success_message = $("div.success-message");

	$("div#add-reward-box input:submit").click(function() {

		// 验证任务
		var isValid = ValidateReward($("form#reward"));

		if (isValid) {
			$("form#treward div.error-message").fadeOut();
			$("div#add-reward-box").hide();

			$success_message.text("A new reward was added!");
			$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
				"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
				.fadeIn().delay(DELAY_TIME).fadeOut();

			$home_name_input.val($home_name_input[0].defaultValue).addClass("default");
			
			php_addreward($("form#reward"),$success_message);
		}
	})
	
	/* 删除奖励 */

	$("input[value=delete]").live("click",function(e) {
		e.stopPropagation();

		$("div#delete-reward-box").show();
		
		verticalMiddle($("div#delete-reward-box").find("div.alert"));
	})

	/* 确认删除奖励 */

	$("div#delete-reward-box input:submit").click(function() {
		$("div#delete-reward-box").hide();
		
		php_deletereward($success_message);

		$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
			"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
			.fadeIn().delay(DELAY_TIME).fadeOut();
	})

	/* 编辑奖励 */

	var $edit_reward_box = $("div#edit-reward-box");
	$("input[value=edit]").live("click",function(e) {
		e.stopPropagation();

		var $li = $(this).parent().parent();
		$edit_reward_box.find("input.reward-name").val($li.find("p.rewards-content span.reward-name").text()).show();
		$edit_reward_box.find("input.set-coin").val($li.find("span.coin").text().replace(/\$/, ""));
		$edit_reward_box.find("input.url").val($li.find("a").attr('href'));

		$edit_reward_box.show();
		verticalMiddle($edit_reward_box.find("div.alert"));
	})

	/* 确认编辑奖励 */

	$("div#edit-reward-box input:submit").click(function() {

		// 验证奖励
		debugger;
		var $form = $edit_reward_box.find("form.reward");
		var isValid = ValidateReward($form);

		if (isValid) {
			$form.find("div.error-message").fadeOut();
			
			php_updatereward($form,$success_message);
			
			$("div#edit-reward-box").hide();
			
			$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
				"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
				.fadeIn().delay(DELAY_TIME).fadeOut();
		}
	})

	/* 确认领取奖励 */

	$("ul.rewards span.coin").live("click",function() {
		debugger;
		$curPoints = $("p.user-coin").text();
		$costPoint = $(this).text();
		
		if($curPoints >= $costPoint)
		{
			$("div#get-reward-box").find("span.cost").text($(this).text()).end().fadeIn();
			verticalMiddle($("div#get-reward-box").find("div.alert"));
		}
		else
		{
			alert("You don't have enough points!");
		}
		
		$operation = $(this).parent().find("div.operation");
		setEditableTaskID($operation.find("input.hidden_id").val())

	})

	$("div#get-reward-box").find(":submit").click(function() {
		var cost = $("div#get-reward-box").find("span.cost").text();

		$("div#get-reward-box").hide();
		
		php_getreward(cost,$success_message);

		$success_message.text("You spent " + cost + "!");
		$success_message.css({"top": (document.documentElement.clientHeight - $success_message.height()) / 2 - 50,
			"left": (document.documentElement.clientWidth - $success_message.width()) / 2})
			.fadeIn().delay(DELAY_TIME).fadeOut();
	})
	
	$("div.user-logout a").live("click",function(){
		php_logout($(this)); 
	})
})

function ValidateReward(form)
{
	var $required = form.find("input.reward-name");
	$required.each(function() {
		if ($(this).val() == "") {
			form.find("div.error-message").text("Please give me a reward name =*v*=").fadeIn();
			return false;
		}
	})
	
	var earn = form.find("input.set-coin").val();
	if (isNaN(earn)) {
		form.find("div.error-message").text("Please give me a number for coins =*v*=").fadeIn();
		return false;
	}
	
	return true;
}

function Validate(form) {
	var isRequiredValid = RequiredValidate(form);
	if (!isRequiredValid) return false;

	var isDateValid = DateValidate(form);
	if (!isDateValid) return false;

	var isEarnValid = EarnValidate(form);
	if (!isEarnValid) return false;

	return true;
}

function RequiredValidate(form) {
	var isValid = true;
	var $required = form.find("input.task-name");
	$required.each(function() {
		if ($(this).val() == "") {
			form.find("div.error-message").text("Please give me a task name =*v*=").fadeIn();
			isValid = false;
		}
	})
	return isValid;
}

function DateValidate(form) {
	var isValid = true;

	var current_date = new Date();
	var d = new Date();

	var year = form.find("select[name=year]").val();
	var month = form.find("select[name=month]").val() - 1;
	var date = form.find("select[name=date]").val();

	d.setFullYear(year, month, date);

	if (d < current_date) {
		form.find("div.error-message").text("Due date must be today or later =*v*=").fadeIn();
		isValid = false;
	}

	return isValid;
}

function EarnValidate (form) {
	var isValid = true;

	var earn = form.find("input.set-coin").val();
	if (isNaN(earn)) {
		form.find("div.error-message").text("Please give me a number for coins =*v*=").fadeIn();
		isValid = false;
	}

	return isValid;
}

/* 使 alert 垂直居中 */

function verticalMiddle(alerter) {
	alerter.css({"top": (document.documentElement.clientHeight - alerter.height()) / 2 - 50,
		"left": (document.documentElement.clientWidth - alerter.width()) / 2});
}



